<?php $this->load->view('admin/permission/link_bar'); ?>
<div style="clear:both"></div>
<form method="post" action="<?php echo base_url(); ?>admin/profile/delete" class="form">
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
    <script language="javascript">
  
        function change_permission(action, id_tab, access){
            document.getElementById('message').innerHTML = "Dang cap nhat";
            var dropdown = document.getElementById('id_profile');
            var id_profile = dropdown.options[dropdown.selectedIndex].value;
           // xajax_change_access(action, id_profile, id_tab, access);
            //alert(action + " " + id_profile + " " + access);
            var url_request = "<?php echo base_url().'admin/permission/change_access/'?>" + action + "/" + id_profile + "/" + id_tab + "/" + access;
            $.ajax({
               type: "POST",
               url: url_request ,
               //data: "name=John&location=Boston",
               dataType: "json",
               success: function(msg){
                 //alert( "Data Saved: " + msg.msg );
                  document.getElementById('message').innerHTML = "Cap nhat xong.";
               },
               error : function(XMLHttpRequest, textStatus, errorThrown) {
                   // $('#waiting').hide(500);
                   // $('#message').removeClass().addClass('error').text('There was an error.').show(500);
                   // $('#demoForm').show(500);
                   alert( "Error: " + textStatus.toString() + url_request);
}
             });
        }
	
        function change_profile(){
            var dropdown = document.getElementById('id_profile');
            var id_profile = dropdown.options[dropdown.selectedIndex].value;
            window.location = "<?php echo base_url() ?>/admin/permission/view/" + id_profile;
        }
    </script>
    <div id="message" style="font-weight:bold; color:#0066FF "></div>
    <div class="box_function">
        <table width="747" border="0" cellpadding="0" cellspacing="0" class="listproduct" id="listproduct">
            <tr>
            <tr>
                <td class="tilte_list_pro">
                    <?php
                    echo $profiles;
                    ?>
                </td>
                <td class="tilte_list_pro">
						View
                </td>
                <td class="tilte_list_pro">
						Add
                </td>
                <td class="tilte_list_pro">
						Edit
                </td>
                <td class="tilte_list_pro">
						Delete
                </td>
            </tr>

            <?php
                    $parents = $tmp[0]; //print_r($parents);
                    //for($i = 0; $i < count($parents); $i++){
                    $i = 0;
                    foreach ($parents as $id => $parent) {
                        $id = $parent->id_tab;
                        $vi_name = $parent->vi_name;
                        $en_name = $parent->en_name;
                        //print_r($access);
                        //echo $access[$id]->view . '<br>';
                        $checked_view = $access[$id]->view == 1 ? 'checked' : '';
                        $checked_add = $access[$id]->add == 1 ? 'checked' : '';
                        $checked_edit = $access[$id]->edit == 1 ? 'checked' : '';
                        $checked_delete = $access[$id]->delete == 1 ? 'checked' : '';
                        $id_profile = $access[$id]->id_profile;

                        if ($i % 2 == 0)
                            $css_class = 'row1';
                        else
                            $css_class = 'row2';

                        $i++;
            ?>
                        <tr onMouseOver="" class="<?php echo $css_class ?>">
                            <td style="padding:5px" class="left">
                                <strong><?php echo $vi_name; ?></strong>
                            </td>

                            <td style="padding:5px" class="pointer center" onclick="document.location = '#'">
                                <input <?php echo $id_profile == 1 ? 'disabled ' : '  ';
                        echo $checked_view ?> type="checkbox" value="<?php echo $id; ?>" name="view<?php echo $id; ?>" id="view<?php echo $id; ?>" onClick="change_permission('view', <?php echo $id; ?>, this.checked)">
                            </td>

                            <td style="padding:5px" class="pointer" onclick="document.location = '#'">
                                <input <?php echo $id_profile == 1 ? 'disabled ' : '  ';
                        echo $checked_add ?> type="checkbox" value="<?php echo $id; ?>" name="add<?php echo $id; ?>" id="add<?php echo $id; ?>" onClick="change_permission('add', <?php echo $id; ?>, this.checked)">
                            </td>

                            <td style="padding:5px" class="pointer" onclick="document.location = '#'">
                                <input <?php echo $id_profile == 1 ? 'disabled ' : '  ';
                        echo $checked_edit ?> type="checkbox" value="<?php echo $id; ?>" name="edit<?php echo $id; ?>" id="edit<?php echo $id; ?>" onClick="change_permission('edit', <?php echo $id; ?>, this.checked)">
                            </td>

                            <td style="padding:5px" class="center">
                                <input <?php echo $id_profile == 1 ? 'disabled ' : '  ';
                        echo $checked_delete ?> type="checkbox" value="<?php echo $id; ?>" name="delete<?php echo $id; ?>" id="delete<?php echo $id; ?>" onClick="change_permission('delete', <?php echo $id; ?>, this.checked)">
                            </td>

                        </tr>

            <?php
                        if (isset($tmp[$id])) {
                            $childs = $tmp[$id]; // is a array.
                            foreach ($childs as $id_child => $child) {
                                //$id = $childs[$j]->id_tab;
                                //$vi_name = $child->vi_name;
                                //echo '____' . $en_name = $child->en_name . '<br>';

                                $id_child = $child->id_tab;
                                $vi_name_child = $child->vi_name;
                                $en_name_child = $child->en_name;

                                $checked_view = '';
                                $checked_add = '';
                                $checked_edit = '';
                                $checked_delete = '';

                                if (isset($access[$id_child])) {
                                    $checked_view = $access[$id_child]->view == 1 ? 'checked' : '';
                                    $checked_add = $access[$id_child]->add == 1 ? 'checked' : '';
                                    $checked_edit = $access[$id_child]->edit == 1 ? 'checked' : '';
                                    $checked_delete = $access[$id_child]->delete == 1 ? 'checked' : '';
                                    $id_profile = $access[$id_child]->id_profile;
                                }
            ?>
                        <tr>
                            <td class="left" style="padding-left:20px ">
<?php echo $vi_name_child; ?>
                            </td>

                            <td class="product center" onclick="document.location = '#'">
                                <input  <?php echo $id_profile == 1 ? 'disabled ' : '  ';
                                echo $checked_view ?> type="checkbox" value="<?php echo $id_child; ?>" name="view<?php echo $id_child; ?>" id="view<?php echo $id_child; ?>" onClick="change_permission('view', <?php echo $id_child; ?>, this.checked)">
                            </td>

                            <td class="product" onclick="document.location = '#'">
                                <input <?php echo $id_profile == 1 ? 'disabled ' : '  ';
                                echo $checked_add ?> type="checkbox" value="<?php echo $id_child; ?>" name="add<?php echo $id_child; ?>" id="add<?php echo $id_child; ?>" onClick="change_permission('add', <?php echo $id_child; ?>, this.checked)">
                            </td>

                            <td class="product" onclick="document.location = '#'">
                                <input <?php echo $id_profile == 1 ? 'disabled ' : '  ';
                                echo $checked_edit ?> type="checkbox" value="<?php echo $id_child; ?>" name="edit<?php echo $id_child; ?>" id="edit<?php echo $id_child; ?>" onClick="change_permission('edit', <?php echo $id_child; ?>, this.checked)">
                                    </td>
 
                                    <td class="product">
                                        <input <?php echo $id_profile == 1 ? 'disabled ' : '  ';
                                echo $checked_delete ?> type="checkbox" value="<?php echo $id_child; ?>" name="delete<?php echo $id_child; ?>" id="delete<?php echo $id_child; ?>" onClick="change_permission('delete', <?php echo $id_child; ?>, this.checked)">
                                    </td>
                                </tr>
<?php
                            }
                        }
?>

<?php
                    }
?>

        </table>
    </div>
</form>